<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/14
 * Time: 18:20
 */

namespace app\index\server;

use app\index\model\Banner as BannerModel;
use app\lib\exception\DatabaseException;
use think\Exception;

class BannerServer extends BaseServer
{
    public function getList($params = [])
    {
        // 参数的准备
        $prep_params = $this->prep_param($params);
        try {
            // 总条数
            $count = BannerModel::count();
            $banner_list = BannerModel::order($prep_params['sort'], $prep_params['order'])
                ->limit($prep_params['offset'], $prep_params['limit'])
                ->select();
        } catch (Exception $e) {
            throw new DatabaseException([
                'msg' => $e->getMessage()
            ]);
        }
        return bootstrap_table_return($count, $banner_list);
    }

    protected function add($params)
    {
        try {
            $result = BannerModel::insert([
                'name' => $params['name'],
                'description' => $params['description'],
                'status' => $params['status']
            ]);
            if(!$result){
                return setMsg(0, '新增Banner位失败');
            }
        }catch (Exception $e) {
            throw new DatabaseException([
                'msg' => $e->getMessage()
            ]);
        }
        return setMsg(1, '新增Banner成功');
    }

    public function detail($id)
    {
        try {
            $detail = BannerModel::get($id);
            if(!$detail) {
                return setMsg(0, '没有查到此Banner信息');
            }
        }catch (Exception $e) {
            throw new DatabaseException([
                'msg' => $e->getMessage()
            ]);
        }
        return setMsg(1, '查找成功', $detail);
    }

    protected function update($params)
    {
        $id = $params['primary_key'];
        // 准备数据
        $data = [
            'name' => $params['name'],
            'description' => $params['description'],
            'status' => $params['status']
        ];
        try {
            $result = BannerModel::where('id', $id)
                ->update($data);
            if(!$result) {
                return setMsg(0, '修改Banner失败');
            }
        } catch (Exception $e) {
            throw new DatabaseException([
                'msg' => $e->getMessage()
            ]);
        }
        return setMsg(1, '修改Banner成功');
    }

    public function changeStatus($id)
    {
        try {
            $info = BannerModel::where('id', $id)
                ->find();
            $info->status = abs(($info->status) - 1);
            $result = $info->save();
            if(!$result) {
                return setMsg(0, '修改此Banner失败');
            }
        }catch (Exception $e) {
            throw new DatabaseException([
                'msg' => $e->getMessage()
            ]);
        }
        return setMsg(1, '修改此Banner成功');
    }
}
